from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

class IndexView(View):
    def get(self, request):
        return render(request, 'index.html')

class DetailView(View):
    def get(self, request, skuId):
        user = request.user
        if user.is_authenticated:
            # 缓存用户的浏览记录
            # 导入模块
            from django_redis import get_redis_connection
            # 获取连接对象
            conn = get_redis_connection("default")
            # 构造存储浏览记录的key
            historyKey = f"history_{user.id}"
            # 移除对应的skuId在缓存中的记录
            conn.lrem(historyKey, 0, skuId)
            # 将skuId重新添加到用户浏览记录缓存中
            conn.lpush(historyKey, skuId)
            # 只保存记录中最新的五条数据-截取
            conn.ltrim(historyKey, 0, 4)
        return render(request, 'detail.html')